Skip to content

CUDAAccelerator.setup_device: fix unrelated device init by matmul precision check#21726

Open
bm371613 wants to merge 2 commits into
Lightning-AI:masterfrom
bm371613:prevent-cuda-init-bad-device-by-check-matmul-precision
Open

CUDAAccelerator.setup_device: fix unrelated device init by matmul precision check#21726
bm371613 wants to merge 2 commits into
Lightning-AI:masterfrom
bm371613:prevent-cuda-init-bad-device-by-check-matmul-precision

Conversation

@bm371613
Copy link
Copy Markdown

Without this fix, CUDAAccelerator.setup_device may initialize an unrelated device, via

  • _check_cuda_matmul_precision
  • _is_ampere_or_later
  • torch.cuda.get_device_capability
  • torch.cuda.get_device_properties
  • torch.cuda._lazy_init

What does this PR do?

Fixes #21725

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

…cision check

Without this fix, CUDAAccelerator.setup_device may initialize an unrelated device, via
- _check_cuda_matmul_precision
- _is_ampere_or_later
- torch.cuda.get_device_capability
- torch.cuda.get_device_properties
- torch.cuda._lazy_init
Copy link
Copy Markdown
Member

@justusschock justusschock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Is there a way for us to test this?

@bm371613
Copy link
Copy Markdown
Author

Great! Is there a way for us to test this?

If you mean unit tests, I can see a simple test monkeypatching torch.cuda and checking that set_device is called before _lazy_init. If you don't mind digging into torch.cuda internals for testing.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 1, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87%. Comparing base (1120456) to head (239e437).
⚠️ Report is 1 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21726   +/-   ##
=======================================
  Coverage      87%      87%           
=======================================
  Files         270      270           
  Lines       23975    23975           
=======================================
  Hits        20750    20750           
  Misses       3225     3225           

@justusschock
Copy link
Copy Markdown
Member

If you mean unit tests, I can see a simple test monkeypatching torch.cuda and checking that set_device is called before _lazy_init. If you don't mind digging into torch.cuda internals for testing.

As long it is local to the specific test and doesn't have side-effects to other tests, i think that sounds like a good approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CUDAAccelerator.setup_device(device) initializes a different device (GPU 0)

3 participants